/********************************************************************
	created:	2008/05/31
	created:	31:5:2008   23:15
	filename: 	x:\Lensow\Loplayer\src\app\SkinControl.cpp
	file path:	x:\Lensow\Loplayer\src\app
	file base:	SkinControl
	file ext:	cpp
	author:		Dean
	
	purpose:	
*********************************************************************/
// SkinControl.cpp : implementation file
//

#include "stdafx.h"
#include "SkinControl.h"
#include "SkinWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CSkinControl
IMPLEMENT_DYNAMIC(CSkinControl, CObject)
//=============================================================================
CSkinControl::CSkinControl()
: m_pSkinWnd(NULL)
, m_Align(None)
, m_nID(0)
, m_cxBorder(0)//(GetSystemMetrics(SM_CXDRAG) + GetSystemMetrics(SM_CXFIXEDFRAME))
, m_cyBorder(0)//(GetSystemMetrics(SM_CYDRAG) + GetSystemMetrics(SM_CYFIXEDFRAME))
{

}

//=============================================================================
CSkinControl::~CSkinControl()
{
}

//=============================================================================
BOOL CSkinControl::CreateCtrl(CSkinWnd* pParent, LPCTSTR lpszCmd, LPCTSTR lpszImg, const CPoint& pt, AlignFlag eFlag, CWnd* pParentWnd, UINT nID )
{
	m_pSkinWnd = pParent;
	ASSERT(m_pSkinWnd);
	m_nID = nID;

	if (!AfxIsValidString(lpszCmd)){
		return FALSE;
	}
	if (!AfxIsValidString(lpszImg)){
		return FALSE;
	}
	m_strCommandName = lpszCmd;
	ASSERT(!m_strCommandName.IsEmpty());

	if (!LoadSkinImage(lpszImg)){
		return FALSE;
	}
	m_Offset = pt;
	m_Align	= eFlag;


	// calculate init position
	CRect rect, rtClient;
	pParentWnd->GetWindowRect(rect);
	pParentWnd->GetClientRect(rtClient);
	m_cxBorder = (rect.Width() - rtClient.Width())/2;
	m_cyBorder = (rect.Width() - rtClient.Width())/2;

	CRect rtTopleft = GetPlacement(rect.Width(), rect.Height());
	pParentWnd->ScreenToClient(&rtTopleft);
	if (!CreateInternal (rtTopleft, pParentWnd, nID)){
		return FALSE;
	}


	return TRUE;
}

//=============================================================================
void CSkinControl::DrawControl( Gdiplus::Bitmap* pbmWnd )
{
	if (NULL == pbmWnd) return;
	//
	const INT cxWnd = pbmWnd->GetWidth();
	const INT cyWnd = pbmWnd->GetHeight();

	CRect rect(GetPlacement(cxWnd, cyWnd));
	DrawIt(pbmWnd, rect);
}

//=============================================================================
CRect CSkinControl::GetPlacement( const INT cxFrame, const INT cyFrame )
{
	CPoint ptTopLeft(0);
	CSize szCtrol(0, 0);
	switch (m_Align)
	{
	case TopLeft:
		ptTopLeft.SetPoint(0, 0);
		break;
	case TopRight:
		ptTopLeft.SetPoint(cxFrame, 0);
		break;
	case BottomLeft:
		ptTopLeft.SetPoint(0, cyFrame);
		break;
	case BottomRight:
		ptTopLeft.SetPoint(cxFrame, cyFrame);
		break;
	case TopCenter:
		ptTopLeft.SetPoint(0, 0);
		break;
	case BottomCenter:
		ptTopLeft.SetPoint(0, cyFrame);
		break;
	default:
		ASSERT(FALSE);
	}
	ptTopLeft += m_Offset;

	return CRect (ptTopLeft, szCtrol);
}

//=============================================================================
void CSkinControl::UpdateSkin()
{
	ASSERT(m_pSkinWnd);
	m_pSkinWnd->UpdateWnd(GetCtrlID());
}

//=============================================================================
void CSkinControl::FireEvent( const CComVariant& vEvent )
{
	ASSERT(m_pSkinWnd);
	m_pSkinWnd->OnEvent(m_strCommandName, vEvent);
}